package main

import (
	"errors"
	"fmt"
)

// 1.实现一个函数，接受若干个float64（用不定长参数），返回这些参数乘积的倒数，除数为0时返回error
// 2.上题用递归实现
// 3.定义两个接口：鱼类和爬行动物，再定义一个结构体：青蛙，同时实现上述两个接口
// 4.实现函数func square(num interface{}) interface{}，计算一个interface{}的平方，interface{}允许是4种类型：float32、float64、int、byte

func square(num interface{}) interface{} {
	var result interface{}
	switch value := num.(type) {
	case float32:
		result = value * value
	case float64:
		result = value * value
	case int:
		result = value * value
	case byte:
		result = value * value
	default:
		result = "Wrong Type!"
	}
	return result
}

type fish interface {
	swim()
}

type reptiles interface {
	crawl()
}

type frog struct {
	Tag string
}

func (f frog) swim() {
	fmt.Println("A frog can swim!")
}

func (f frog) crawl() {
	fmt.Println("A frog can swim!")

}

func count_inverse(nums ...float64) interface{} {
	var result float64 = 1
	for _, num := range nums {
		result *= num
	}
	if result <= 0 {
		return errors.New("除数为0")
	}
	return 1 / result
}

func recursion(nums ...float64) float64 {
	n := len(nums)
	if nums[n-1] == 0 {
		return -1
	} else if n == 1 {
		return 1 / nums[0]
	} else {
		return 1 / nums[n-1] * recursion(nums[:n-1]...)
	}
}

func main() {
	fmt.Println("====第一题====")
	result1 := count_inverse(10, 4.5, 6.5)
	fmt.Println(result1)

	fmt.Println("====第二题====")
	result2 := recursion(10, 4.5, 6.5)
	fmt.Println(result2)

	fmt.Println("====第三题====")
	f := frog{
		Tag: "frog",
	}
	f.swim()
	f.crawl()

	fmt.Println("====第四题====")
	fmt.Println(square('0'))
}
